﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ScriptIT
{
    public partial class frmConfig : Form
    {
        bool bFormLoading = false;
        public frmConfig()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Options.IsOracle = this.rbOracle.Checked;
            Options.OutPutType = cbOutputType.SelectedIndex;
            Options.OutPutLocation = cbDefLoc.SelectedIndex;
            Options.OverwriteFile = chkOverwriteFile.Checked;
            Options.OpenExportFile = chkOpen.Checked;
            Options.SaveOptions();
            this.Close();
        }

        private void frmConfig_Load(object sender, EventArgs e)
        {
            bFormLoading = true;
            if (Options.IsOracle)
            {
                this.rbOracle.Checked = true;
                this.rbSQL.Checked = false;
            }
            else
            {
                this.rbOracle.Checked = false;
                this.rbSQL.Checked = true;
            }
            this.cbOutputType.SelectedIndex = Options.OutPutType;
            this.cbDefLoc.SelectedIndex = Options.OutPutLocation;

            this.rbOracle.AutoCheck = true;
            this.rbSQL.AutoCheck = true;
            chkOpen.Checked = Options.OpenExportFile;
            chkOverwriteFile.Checked = Options.OverwriteFile;
            cbDefLoc.Items[0] = "Last Used Folder (" +  DirectoryName(Options.LastUsedFoler)+ ")";
            cbDefLoc.Items[3] = "Custom Folder (" + DirectoryName(Options.CustomPath) + ")";

            switch ((Options.eDefaultLocType)Options.OutPutLocation)
            {
                case (Options.eDefaultLocType.Custom):
                    {
                        toolTipDefLocation.SetToolTip(cbDefLoc, Options.CustomPath);
                        break;
                    }
                case (Options.eDefaultLocType.LastUsedFolder):
                    {
                        toolTipDefLocation.SetToolTip(cbDefLoc, Options.LastUsedFoler);
                        break;
                    }
                default:
                    {
                        toolTipDefLocation.SetToolTip(cbDefLoc, "");
                        break;
                    }
            }
            bFormLoading = false;
        }

        private string DirectoryName(string path)
        {
            string sRetValue = new DirectoryInfo(path).Name;
            return sRetValue;
        }

        private void cbOutputType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!(cbOutputType.SelectedIndex == 0 || cbOutputType.SelectedIndex == 1))
            {
                chkOpen.Checked = false;
                chkOpen.Enabled = false;
            }
            else
                chkOpen.Enabled = true;
        }

        private void chkOverwriteFile_CheckedChanged(object sender, EventArgs e)
        {
            lblOverWriteFile.Visible = !chkOverwriteFile.Checked;
        }

        private void cbDefLoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (bFormLoading)
                return;
            if (cbDefLoc.SelectedIndex == 3)
            {
                dlgSavePath.SelectedPath = Options.LastUsedFoler;
                dlgSavePath.Description = "Select a new custom path or click Cancel to use previous value.";
                if (dlgSavePath.ShowDialog() == DialogResult.OK)
                {
                    Options.CustomPath = dlgSavePath.SelectedPath;
                    bFormLoading = true;
                    cbDefLoc.Items[3] = "Custom Folder (" + DirectoryName(Options.CustomPath) + ")";
                    bFormLoading = false;
                }
                toolTipDefLocation.SetToolTip(cbDefLoc, Options.CustomPath);
            }
            else if(cbDefLoc.SelectedIndex == 0)
                toolTipDefLocation.SetToolTip(cbDefLoc, Options.LastUsedFoler);
            else
                toolTipDefLocation.SetToolTip(cbDefLoc, "");

        }
    }
}
